package org.gitee.weapp.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.gitee.weapp.service.GoodsService;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;

@Slf4j
@Service
public class GoodsServiceImpl implements GoodsService {

    @Override
    public void getGoodsPic(Integer goodsId, HttpServletResponse response) {
        File avatarPath = new File(new File("").getAbsolutePath() + "/goods");
        if (avatarPath.exists()) {
            try(InputStream input = Files.newInputStream(Paths.get(
                    avatarPath.getAbsolutePath() + '/' + goodsId + ".jpg"));
                ServletOutputStream output = response.getOutputStream()) {
                IOUtils.copy(input,output);
            } catch (Exception e) {
                log.error("获取商品图片出错:", e);
            }
        }
    }
}
